package com.ugoodtech.mdcc.web.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.AnswerDic;
import com.ugoodtech.mdcc.core.domain.QQuestionAnswerOption;
import com.ugoodtech.mdcc.core.domain.QuestionAnswerOption;
import com.ugoodtech.mdcc.core.repository.AnswerDicRepository;
import com.ugoodtech.mdcc.core.repository.QuestionAnswerOptionRepository;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.AnswerDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerDicServiceImpl implements AnswerDicService {

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private AnswerDicRepository answerDicRepository;

    @Override
    public JsonResponse addRadio(Long answerId, Long questionId) {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(questionId));
        List<QuestionAnswerOption> answerOptionList = (List<QuestionAnswerOption>) questionAnswerOptionRepository.findAll(builder);
        for (QuestionAnswerOption answerOption : answerOptionList) {
            if (answerOption.getId().equals(answerId)) {
                return addAnswerDic(answerId, answerOption.getAdvice(), answerOption.getEvaluation(), true);
            } else {
                return addAnswerDic(answerId, answerOption.getAdvice(), answerOption.getEvaluation(), false);
            }
        }
        return null;
    }

    @Override
    public JsonResponse addCheckBox(String[] ids, Long questionId) {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(questionId));
        List<QuestionAnswerOption> answerOptionList = (List<QuestionAnswerOption>) questionAnswerOptionRepository.findAll(builder);
        for (String answerId : ids) {
            for (QuestionAnswerOption answerOption : answerOptionList) {
                if (answerId.equals(answerOption.getId())) {
                    addAnswerDic(Long.valueOf(answerId), answerOption.getAdvice(), answerOption.getEvaluation(), true);
                } else {
                    addAnswerDic(Long.valueOf(answerId), answerOption.getAdvice(), answerOption.getEvaluation(), false);
                }
            }
        }
        return JsonResponse.successResponse();
    }


    private JsonResponse addAnswerDic(Long answerId, String advice, String evaluation, boolean expression) {
        AnswerDic answerDic = answerDicRepository.findOne(answerId);
        if (answerDic == null) {
            answerDic = new AnswerDic();
            answerDic.setAnswerId(answerId);
            answerDic.setAdvice(advice);
            answerDic.setEvaluation(evaluation);
            if (expression == true) {
                answerDic.setExpression("笑脸");
            } else {
                answerDic.setExpression("哭脸");
            }
            answerDicRepository.save(answerDic);
            return JsonResponse.successResponse();
        }
        return null;
    }


}
